<template>
    <div class="content">
        <div class="clearfix">
            <Form class="f-fl" inline :label-width="5">
                <FormItem>
                    <Input
                        v-model="params.schoolName"
                        placeholder="学校名称"
                    ></Input>
                </FormItem>
                <FormItem>
                    <Select
                        style="width: 140px;"
                        placeholder="状态"
                        v-model="params.status"
                        clearable
                    >
                        <Option :value="1">可用</Option>
                        <Option :value="0">禁用</Option>
                    </Select>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="search">搜索</Button>
                </FormItem>
                <FormItem>
                    <Button @click="clearSearch">清空</Button>
                </FormItem>
            </Form>
            <div class="f-fr">
                <Button type="primary" @click="handleAddSchool">新建学校</Button>
            </div>
        </div>
        <Table :data="list" :columns="columns" border align="center">
            <template slot-scope="{ row }" slot="schoolName">
                <div>{{ row.schoolName }}</div>
            </template>
            <template slot-scope="{ row }" slot="contacts">
                <div>{{ row.contacts }}</div>
            </template>
            <template slot-scope="{ row }" slot="tel">
                <div>{{ row.tel }}</div>
            </template>
            <template slot-scope="{ row }" slot="enable">
                <div>{{ row.enable ? "可用" : "禁用" }}</div>
            </template>
            <template slot-scope="{ row }" slot="createTime">
                <div>{{ row.createTime }}</div>
            </template>
            <template slot-scope="{ row }" slot="action">
                <div class="table-action">
                    <Button type="text" @click="handleEditSchool(row.schoolId)">编辑</Button>
                    <Button type="text" @click="handlerUser(row)">查看用户</Button>
                    <Button type="text" @click="handleDelete(row.schoolId)">删除</Button>
                </div>
            </template>
        </Table>
        <Pagination
            :pageSize.sync="params.pageSize"
            :current.sync="params.pageNum"
            :total="params.total"
            @change="queryList"
        />
    </div>
</template>
<script>
import { schoolManageList, deleteSchool } from '@/api/schoolManage'
import formatTime from '@/libs/formatTime'
export default {
    inject: ['nextPageReload'],
    data () {
        return {
            params: {
                total: 0,
                status: '',
                pageNum: 1,
                pageSize: 10,
                schoolName: ''
            },
            list: [],
            columns: Object.freeze([
                { title: '序号', type: 'index', align: 'center', width: 80 },
                { title: '学校名称', slot: 'schoolName', align: 'center' },
                { title: '联系人', slot: 'contacts', align: 'center' },
                { title: '联系电话', slot: 'tel', align: 'center' },
                { title: '状态', slot: 'enable', align: 'center' },
                { title: '创建时间', slot: 'createTime', align: 'center' },
                { title: '操作', slot: 'action', align: 'center', width: 150 }
            ])
        }
    },
    mounted () {
        this.queryList()
    },
    methods: {
        search () {
            this.params.pageNum = 1
            this.queryList()
        },
        queryList () {
            const enable = typeof this.params.status !== 'number' ? '' : this.params.status === 1
            schoolManageList({
                ...this.params,
                enable
            }).then(res => {
                if (res.data.code === '0000') {
                    const data = res.data.data
                    data.list.forEach(item => {
                        item.createTime = formatTime.fullTime(item.createTime)
                    })
                    this.params.total = data.total
                    this.list = data.list
                } else {
                    this.$Message.warning(res.data.msg)
                }
            })
        },
        clearSearch () {
            this.params = {
                total: 0,
                status: '',
                pageNum: 1,
                pageSize: 10,
                schoolName: ''
            }
            this.queryList()
        },
        handleDelete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '将同步删除该院校所有账号，确认是否删除？',
                onOk: () => {
                    deleteSchool(id).then(res => {
                        if (res.data.code === '0000') {
                            this.$Message.success('删除成功')
                            this.params.pageNum = 1
                            this.queryList()
                        } else {
                            this.$Message.error(res.data.msg)
                        }
                    })
                }
            })
        },
        handleAddSchool () {
            this.nextPageReload()
            this.$router.push({
                path: '/schoolManage/add'
            })
        },
        handleEditSchool (id) {
            this.nextPageReload()
            this.$router.push({
                name: 'schoolManageUpdate',
                params: {
                    id
                }
            })
        },
        handlerUser (row) {
            this.nextPageReload()
            this.$router.push({
                name: 'userManageList',
                query: {
                    schoolId: row.schoolId,
                    schoolName: row.schoolName
                }
            })
        }
    }
}
</script>
